Skip to content

Refuse a device operand for a host operator; MoDL lesson on the card - #173

Merged
mcencini merged 4 commits into
mainfrom
claude/project-thread-n9v447
Nov 12, 2026
Merged

mcencini merged 4 commits into
mainfrom
claude/project-thread-n9v447

Conversation

@mcencini

@mcencini mcencini commented Nov 12, 2026 •

Copy link
Copy Markdown
Collaborator

What changed

  • Applying an operator (Operator._apply) and running the linear solve (optim._linear._solve) now raise ValueError when the operand is on a card but the operator's data is on the host. The message names the device and says how to fix it.
  • These are still accepted:
    • An operator built on a card (device= or data on the card) takes host operands, which it moves itself.
    • An operator that holds no data (device is None, e.g. FFT) takes operands anywhere.
  • In the solve, x0 must be on y's device, and a bare cuda counts as the current card.
  • The MoDL lesson (docs/examples/06-learning/02-modl-with-admm.py) keeps the SENSE operator, its data and the network on one device: the card when there is one, the host otherwise. Lightning's accelerator follows that device.

Why

A CartesianSense built on host tensors crashes the process (SIGSEGV in _apply / _solve) when it is applied to CUDA tensors or solved with optim.cg. This was reproduced with bartorch 0.0.22 on an A40, on forward, adjoint, normal and cg. It is what stopped the MoDL lesson from running with accelerator="gpu". With the lesson as changed here, the whole example runs in 52 s on an A40, against 175 s on the CPU.

Validation

  • ./scripts/lint.sh: all checks passed.
  • CPU suite (./scripts/run_tests.sh -n 4, clang 18 build, first head): 2567 passed, 183 skipped. The two test_openmp.py::...share_one_openmp_runtime failures are unrelated: they fail the same way without this change, because this container's clang build links LLVM's libomp while torch carries libgomp.
  • At 31b0474 on the CPU, tests/test_cuda.py tests/test_sense.py tests/test_solve.py tests/test_optim_functional.py tests/test_linop.py: 282 passed, 27 skipped.
  • On an NVIDIA A40 at 31b0474 (this PR's Python over a 0.0.22 install):
    • tests/test_cuda.py tests/test_sense.py: 104 passed, 1 skipped. This includes test_an_operator_on_a_card_takes_and_returns_host_arrays, which the first head failed.
    • scripts/check_device.py: 9/9.
  • The docs build runs in CI.

Documentation impact

The MoDL lesson runs on a GPU when one is present. A device tensor given to a host-built operator raises ValueError instead of crashing.

🤖 Generated with Claude Code

https://claude.ai/code/session_01MFB6QLvu9frE7HdzPd4yCU

Applying a CartesianSense built on host tensors to a CUDA tensor, or solving
with it, crashed the process. Operator application and the linear solve now
raise a ValueError naming both devices. The MoDL lesson keeps the operator,
its data and the network on one device, the card when there is one.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01MFB6QLvu9frE7HdzPd4yCU
@codecov

codecov Bot commented Nov 12, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 88.88889% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/bartorch/optim/_linear.py 75.00% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@mcencini mcencini changed the title Refuse an operand on another device than the operator's data; MoDL lesson on the card Refuse a device operand for a host operator; MoDL lesson on the card Nov 12, 2026

Copy link
Copy Markdown
Collaborator Author

codecov/patch: at e9e4204 the duplicate device helper is gone, and check_device is fully exercised on the host through meta-device tests. The one line left uncovered is the x0-vs-y device refusal in _solve. Reaching it needs a CUDA tensor, which the hosted runners don't have. It is covered by test_a_starting_image_on_another_device_than_the_data_is_refused, and that test ran on an A40 alongside the rest of tests/test_cuda.py / tests/test_sense.py (104 passed, 1 skipped at 31b0474).


Generated by Claude Code

@mcencini
mcencini merged commit 980ec97 into main Nov 12, 2026
15 of 18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants